package com.av.recorder.audiorecord;

import android.media.MediaRecorder;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class AudioManager {
    private MediaRecorder mRecorder;
    private String mDirString;
    private String mCurrentFilePathString;
    private boolean isPrepared;
    private static AudioManager mInstance;
    public AudioManager.AudioStageListener mListener;

    private AudioManager(String dir) {
        this.mDirString = dir;
    }

    public static AudioManager getInstance(String dir) {
        if (mInstance == null) {
            Class var1 = AudioManager.class;
            synchronized(AudioManager.class) {
                if (mInstance == null) {
                    mInstance = new AudioManager(dir);
                }
            }
        }

        return mInstance;
    }

    public void setOnAudioStageListener(AudioManager.AudioStageListener listener) {
        this.mListener = listener;
    }

    public boolean prepareAudio() {
        try {
            this.isPrepared = false;
            File dir = new File(this.mDirString);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            String fileNameString = this.generalFileName();
            File file = new File(dir, fileNameString);
            this.mCurrentFilePathString = file.getAbsolutePath();
            this.mRecorder = new MediaRecorder();
            this.mRecorder.setOutputFile(file.getAbsolutePath());
            this.mRecorder.setAudioSource(1);
            this.mRecorder.setOutputFormat(3);
            this.mRecorder.setAudioEncoder(1);
            this.mRecorder.prepare();
            this.mRecorder.start();
            this.isPrepared = true;
            if (this.mListener != null) {
                this.mListener.wellPrepared();
            }

            return true;
        } catch (IllegalStateException var4) {
            var4.printStackTrace();
        } catch (IOException var5) {
            var5.printStackTrace();
        }

        return false;
    }

    private String generalFileName() {
        return UUID.randomUUID().toString() + ".mp3";
    }

    public float getVoiceLevel(int maxLevel) {
        if (this.isPrepared) {
            try {
                double ratio = (double)this.mRecorder.getMaxAmplitude() / 1.0D;
                double db = 0.0D;
                if (ratio > 1.0D) {
                    db = 20.0D * Math.log10(ratio);
                }

                return (float)db / 100.0F;
            } catch (Exception var6) {
            }
        }

        return 0.0F;
    }

    public void release() {
        if (null != this.mRecorder) {
            this.mRecorder.stop();
            this.mRecorder.release();
            this.mRecorder = null;
        }

    }

    public void cancel() {
        this.release();
        if (this.mCurrentFilePathString != null) {
            File file = new File(this.mCurrentFilePathString);
            file.delete();
            this.mCurrentFilePathString = null;
        }

    }

    public String getCurrentFilePath() {
        return this.mCurrentFilePathString;
    }

    public interface AudioStageListener {
        void wellPrepared();
    }
}
